package com.movistar.mas;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.content.ContentResolver;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void actualizar (View view){
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
		    while (cur.moveToNext()) {
		        String id = cur.getString(
	                        cur.getColumnIndex(ContactsContract.Contacts._ID));
		        Log.v("id", id);
		        String name = cur.getString(
	                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		        Log.v("name", name);
		        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
		        	Cursor pCur = cr.query(
		         		    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
		         	        while (pCur.moveToNext()) {
		         		    // Do something with phones
		         	        	
		         	        	String num = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
		         	        	Log.v("num", num);
		         	        	
		         	        	String cad="", pp="", sp="", codpa="", coddos="", codtres="";
		         	           int tam=0;
		         	           //for (int i = 0; i < num.length; i++) {
		         	               if (num.charAt(3)=='-') {
		         	                   cad = num.substring(0, 3) + num.substring(4, 7) + num.substring(8);
		         	               }else{
		         	                   cad = num;
		         	               }
		         	               tam = cad.length();
		         	               if (tam==9) {
		         	                   coddos = cad.substring(0,2);
		         	                   codtres = cad.substring(0,3);
		         	                   if (coddos.equals("09")|coddos.equals("08")|codtres.equals("039")|codtres.equals("059")|codtres.equals("069")|codtres.equals("079")) {                
		         	                       num= cambiar(cad, tam);
		         	                   }
		         	               } else if(tam==12){
		         	                   codpa = cad.substring(0, 4);
		         	                   if (codpa.equals("+593")) {
		         	                       coddos = cad.substring(4,5);
		         	                       codtres = cad.substring(4,6);
		         	                       if (coddos.equals("9")|coddos.equals("8")|codtres.equals("39")|codtres.equals("59")|codtres.equals("69")|codtres.equals("79")) {
		         	                           num= cambiar(cad, tam);
		         	                       }
		         	                   }
		         	               }
		         	           //}
		         	           Log.v("numero Actulizado", num);
		         	        } 
		         	        pCur.close();
	 	        }
	        }
	 	}
    }
    
    public String cambiar(String cad, int tam){
        String pp="", sp="";
        pp = cad.substring(0, tam-8);
        sp = cad.substring(tam-8);
        return pp + "9" +sp;
    }
}
